/*
* @author Oleg Varaksin
* $$Id: ProcessFaceletsMojo.java 50 2011-01-29 00:24:11Z mcaliskan $$
*/

package com.googlecode.jsf2inspector.plugin.inspection;

import com.googlecode.jsf2inspector.plugin.commons.AbstractProcessMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.DirectoryScanner;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

/**
 * @goal processFacelets
 * @phase process-resources
 */
public class ProcessFaceletsMojo extends AbstractProcessMojo<File>
{
    private Set<File> faceletsFiles;

    /**
    * @parameter
    */
    private UnwantedTagInspector unwantedTagInspector;

    /**
    * @parameter
    */
    private UnwantedAttributeInspector unwantedAttributeInspector;

    /**
    * @parameter
    */
    private RequestedAttributeInspector requestedAttributeInspector;

    public void setUnwantedTagInspector(UnwantedTagInspector unwantedTagInspector) {
        this.unwantedTagInspector = unwantedTagInspector;
        addInspector(unwantedTagInspector);
    }

    public void setUnwantedAttributeInspector(UnwantedAttributeInspector unwantedAttributeInspector) {
        this.unwantedAttributeInspector = unwantedAttributeInspector;
        addInspector(unwantedAttributeInspector);
    }

    public void setRequestedAttributeInspector(RequestedAttributeInspector requestedAttributeInspector) {
        this.requestedAttributeInspector = requestedAttributeInspector;
        addInspector(requestedAttributeInspector);
    }

    public Set<File> getFaceletsFiles() throws MojoExecutionException {
        if (faceletsFiles != null) {
            return faceletsFiles;
        }

        faceletsFiles = new HashSet<File>();

        DirectoryScanner directoryScanner = new DirectoryScanner();
        directoryScanner.setBasedir(webappDirectory);
        String[] includes = {"**/*.xhtml"};
        directoryScanner.setIncludes(includes);

        try {
            getLog().debug("Scanning xhtml files under webapp folder ...");
            directoryScanner.scan();
        } catch (IllegalStateException e) {
            throw new MojoExecutionException("Error while scanning xhtml files under webapp folder", e);
        }

        for (String fileName : directoryScanner.getIncludedFiles()) {
            File configFile = new File(webappDirectory, fileName);
            faceletsFiles.add(configFile);
        }

        return faceletsFiles;
    }
}
